package com.nageoffer.jobtrain.admin.service.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.util.ObjectUtil;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.nageoffer.jobtrain.admin.common.biz.user.UserContext;
import com.nageoffer.jobtrain.admin.dao.entity.OjQuestionDO;
import com.nageoffer.jobtrain.admin.dao.mapper.OjQuestionMapper;
import com.nageoffer.jobtrain.admin.dto.req.OjQuestionAddReqDTO;
import com.nageoffer.jobtrain.admin.dto.req.OjQuestionEditReqDTO;
import com.nageoffer.jobtrain.admin.dto.req.OjQuestionPageReqDTO;
import com.nageoffer.jobtrain.admin.dto.resp.OjQuestionDetailRespDTO;
import com.nageoffer.jobtrain.admin.dto.resp.OjQuestionPageRespDTO;
import com.nageoffer.jobtrain.admin.service.OjQuestionService;
import com.nageoffer.jobtrain.admin.service.chain.OjQuestionChainContext;
import com.nageoffer.jobtrain.admin.toolkit.SplitStrUtils;
import com.nageoffer.jobtrain.common.web.exception.ClientException;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;

import java.util.List;

import static com.nageoffer.jobtrain.admin.common.enums.ChainBizMarkEnum.OJ_QUESTION_CREATE_MARK;

/**
 * author：唐晨
 * Oj题目接口实现层
 */
@Service
@RequiredArgsConstructor
public class OjQuestionServiceImpl extends ServiceImpl<OjQuestionMapper, OjQuestionDO> implements OjQuestionService {
    private final OjQuestionMapper ojQuestionMapper;
    private final OjQuestionChainContext ojQuestionChainContext;

    @Override
    public IPage<OjQuestionPageRespDTO> ojQuestionPageList(OjQuestionPageReqDTO requestParam) {
        Page<OjQuestionPageRespDTO> page = new Page<>(requestParam.getCurrent(), requestParam.getSize());
        if (StrUtil.isNotEmpty(requestParam.getIdStr())) {
            requestParam.setOjQuestionList(SplitStrUtils.splitOjIdList(requestParam.getIdStr()));
        }
        List<OjQuestionPageRespDTO> result = baseMapper.ojQuestionPageList(requestParam.getDifficulty(),
                requestParam.getTitle(), requestParam.getOjQuestionList(), page);
        page.setRecords(result);
        return page;
    }

    @Override
    public OjQuestionDO hasOjQuestionDetail(Long id) {
        LambdaQueryWrapper<OjQuestionDO> queryWrapper = Wrappers.lambdaQuery(OjQuestionDO.class)
                .eq(OjQuestionDO::getId, id)
                .eq(OjQuestionDO::getDelFlag, 0);
        return ojQuestionMapper.selectOne(queryWrapper);
    }

    @Override
    public void addOjQuestion(OjQuestionAddReqDTO requestParam) {
        ojQuestionChainContext.handler(OJ_QUESTION_CREATE_MARK.name(), requestParam);
        OjQuestionDO ojQuestionDO = BeanUtil.toBean(requestParam, OjQuestionDO.class);
        ojQuestionDO.setCreateOperator(Long.valueOf(UserContext.getAdminUserId()));
        try {
            int insert = ojQuestionMapper.insert(ojQuestionDO);
            if (insert < 1) {
                throw new ClientException("新增Oj题目失败");
            }
        } catch (Throwable ex) {
            throw new ClientException("新增Oj题目异常");
        }
    }

    @Override
    public OjQuestionDetailRespDTO getOjQuestionDetailById(Long id) {
        OjQuestionDO ojQuestionDO = hasOjQuestionDetail(id);
        if (ObjectUtil.isEmpty(ojQuestionDO)) {
            throw new ClientException("请求确定该Oj题详情是否存在");
        }
        return BeanUtil.toBean(ojQuestionDO, OjQuestionDetailRespDTO.class);
    }

    @Override
    public void updateOjQuestionDetail(OjQuestionEditReqDTO requestParam) {
        OjQuestionDO ojQuestionDO = hasOjQuestionDetail(requestParam.getId());
        if (ObjectUtil.isEmpty(ojQuestionDO)) {
            throw new ClientException("请求确定该Oj题详情是否存在");
        }
        LambdaUpdateWrapper<OjQuestionDO> updateWrapper = Wrappers.lambdaUpdate(OjQuestionDO.class)
                .eq(OjQuestionDO::getId, requestParam.getId())
                .eq(OjQuestionDO::getDelFlag, 0);
        ojQuestionDO = BeanUtil.toBean(requestParam, OjQuestionDO.class);
        ojQuestionDO.setUpdateOperator(Long.valueOf(UserContext.getAdminUserId()));
        baseMapper.update(ojQuestionDO, updateWrapper);
    }

    @Override
    public void deleteOjQuestionDetail(Long id) {
        if (ObjectUtil.isEmpty(hasOjQuestionDetail(id))) {
            throw new ClientException("请确保该Oj题是否存在");
        }
        LambdaUpdateWrapper<OjQuestionDO> updateWrapper = Wrappers.lambdaUpdate(OjQuestionDO.class)
                .eq(OjQuestionDO::getId, id)
                .eq(OjQuestionDO::getDelFlag, 0);
        OjQuestionDO ojQuestionDO = new OjQuestionDO();
        ojQuestionDO.setDelFlag(1);
        baseMapper.update(ojQuestionDO, updateWrapper);
    }
}
